<div class="form">
    <?php echo validation_errors(); ?>
    
    <?php echo form_open($action); ?>
        <div id="tabs">
            <ul>
                <li><a href="#tabs-1">基本信息</a></li>
                <li><a href="#tabs-2">联系信息</a></li>
                <li><a href="#tabs-3">文件录入</a></li>
            </ul>
            <div id="tabs-1">
                <div>
                    <label class="span-4">名称（中文）<span class="required">*</span></label>
                    <input class="text span-17" id="c_name" name="c_name" 
                           value="<?php echo set_value('c_name', $client['c_name']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">名称（英文）</label>
                    <input class="text span-17" id="c_name_en" name="c_name_en" 
                           value="<?php echo set_value('c_name_en', $client['c_name_en']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">地址（中文）<span class="required">*</span></label>
                    <input class="text span-17" id="c_addr" name="c_addr" 
                           value="<?php echo set_value('c_addr', $client['c_addr']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">地址（英文）</label>
                    <input class="text span-17" id="c_addr_en" name="c_addr_en" 
                           value="<?php echo set_value('c_addr_en', $client['c_addr_en']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">邮寄地址（中文）</label>
                    <input class="text span-17" id="c_mailing_addr" name="c_mailing_addr" 
                           value="<?php echo set_value('c_mailing_addr', $client['c_mailing_addr']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">邮寄地址（英文）</label>
                    <input class="text span-17" id="c_mailing_addr_en" name="c_mailing_addr_en" 
                           value="<?php echo set_value('c_mailing_addr_en', $client['c_mailing_addr_en']); ?>" />
                    <div class="clear"></div>
                </div>
                <div>
                    <label class="span-4">证照类型<span class="required">*</span></label>
                    <?php echo form_dropdown('c_licence_type', $licence_types, set_value('c_licence_type', $client['c_licence_type']), 'class="span-4" id="c_licence_type"'); ?>
                    <label class="span-3">证照编号<span class="required">*</span></label>
                    <input class="text span-4" id="c_licence_number" name="c_licence_number" 
                           value="<?php echo set_value('c_licence_number', $client['c_licence_number']); ?>" />
                    <label class="span-2">国家</label>
                    <input class="text span-4" id="c_country" name="c_country" 
                           value="<?php echo set_value('c_country', $client['c_country']); ?>" />
                    <div class="clear"></div>
                </div>
            </div>
            <div id="tabs-2">
                <?php if (isset($contacts_html)) echo $contacts_html; ?>
                <button type="button" onclick="newContact(this);">新增联系人</button>
            </div>
            <div id="tabs-3">
                <?php if (isset($documents_html)) echo $documents_html; ?>
                <button type="button" id="add_document">新增</button>
            </div>
        </div>
        <div class="form-actions">
            <input type="submit" class="form-submit" value="保存" />
        </div>
    </form>
</div>
<script>
    function newContact(sender) {
        var element = $('<?php echo $empty_contact_html; ?>');
        element.find("button:not(.adhere)").button();
        $(sender).before(element);
    }
    function delContact(sender) {
        $(sender).parents('fieldset').remove();
    }
    
    function assignEvent(container) {
        $(container).find("button:not(.adhere)").button();
        $(container).find(".normal-date").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: "yy-mm-dd",
        });
        $(container).find(".image-link").magnificPopup({type:'image'});

        /* 官方文件 */
        $(container).find("input[name^=doc_original_name]").yeFileUpload({
            url: "<?php echo site_url('upload/do_upload'); ?>",
            nameAttr: "this",
            pathAttr: "next",
            maxSize: "5MB"
        });
        $(container).find('button.remove-document').click(function() {
            $(this).parent().remove();
        });
    }

    $(function() {
        $("#tabs").tabs();
        assignEvent(document);
        
        /* 官方文件 */
        $("#add_document").click(function() {
            var element = $('<?php echo $empty_document_html; ?>');
            assignEvent(element);

            $(this).before(element);
        });
    });
</script>
